<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ServicePackDetail_Model extends CI_Model{
	protected $_name = "service_pack_detail";
	protected $_primary = "SpdId";
	var $SpId = "SpId", $Duration = "Duration", $Price = "Price", $PriceExtension = "PriceExtension";
	
	function getServicePackDetailBySpId($SpId){
		$this->db->where($this->SpId, $SpId);
		$query = $this->db->get($this->_name);
		return $query->result();
	}
	
	function insertServicePackDetail($SpId){
		$i=0;
		$arrDuration = $this->input->post('Duration', true);
		$arrPrice = $this->input->post('Price', true);
		$arrPriceExtension = $this->input->post('PriceExtension', true);
		foreach($arrDuration as $duration){
			if($this->getExistServicePackDetail($SpId, $duration) == 0){
				$this->db->set($this->SpId, $SpId);
				$this->db->set($this->Duration, $duration);
				$this->db->set($this->Price, trim(str_replace(".","",$arrPrice[$i])));
				$this->db->set($this->PriceExtension, trim(str_replace(".","",$arrPriceExtension[$i])));
				$this->db->insert($this->_name);
			}
			$i++;
		}
	}
	function getExistServicePackDetail($SpId, $Duration){
		$this->db->where($this->SpId, $SpId);
		$this->db->where($this->Duration, $Duration);
		$query = $this->db->get($this->_name);
		return $query->num_rows();
	}
	
	function deleteSPDByDuration($duration){
		$this->db->where($this->Duration, $duration);
		$this->db->delete($this->_name);
	}
	
}